//
//  DENGRUViewController.m
//  Candy
//
//  Created by DMT on 2018/3/19.
//  Copyright © 2018年 庄文盛 All rights reserved.
//

#import "DENGRUViewController.h"
#import "YSHYSlideViewController.h"
#import "leftNav.h"
#import "mainView.h"
#import "Design.h"
#import <BmobSDK/Bmob.h>
#import <AFNetworking.h>
#import "XHToast.h"
#import "UserModel.h"
#import "Header.h"
#import <JSONModel.h>
#import <MJExtension.h>

@interface DENGRUViewController ()
@property (weak, nonatomic) IBOutlet UIButton *load;
@property (weak, nonatomic) IBOutlet UITextField *user;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation DENGRUViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _load.layer.masksToBounds=YES;
    _load.layer.cornerRadius=_load.bounds.size.height/2;
    [_load addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchDown];
    _user.layer.masksToBounds=YES;
    _user.layer.cornerRadius=_user.bounds.size.height/2;
    _password.layer.masksToBounds=YES;
    _password.layer.cornerRadius=_password.bounds.size.height/2;
    [_user setValue:[UIFont systemFontOfSize:20] forKeyPath:@"_placeholderLabel.font"];
     [_password setValue:[UIFont systemFontOfSize:20] forKeyPath:@"_placeholderLabel.font"];
}

-(void)jump{
    
    AFHTTPSessionManager* session=[AFHTTPSessionManager manager];
    session.responseSerializer=[AFHTTPResponseSerializer serializer];
     NSDictionary *parameters = @{@"password":self.password.text,@"email":self.user.text};
    [session GET:@"http://127.0.0.1:3000/login" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"下载成功");
        
        NSString *source = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"res=%@",source);
        if (![source isEqualToString:@"密码错误"]) {
            UserModel *model = [UserModel mj_objectWithKeyValues:source];
            NSLog(@"%@",model);
            [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:model.id] forKey:USERID];
            
            Design *design = [Design new];
            [[YSHYSlideViewController shareInstance] setMainViewController:design];
            self.view.window.rootViewController = [YSHYSlideViewController shareInstance];
        }
        else{
            [XHToast showCenterWithText:source];
        }
      
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"下载失败");
    }];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
